<?php
class Admin_Model_FeatureDetail extends Zend_Db_Table_Abstract {
	protected $_name = 'feature_detail';
	protected $_primary = 'id';
	protected $_dependentTables = array('Admin_Model_ProductFeatureDetail');
	protected $_referenceMap = array(
			'feature'=>array(
					'columns' => array('featureId'),
					'refTableClass' => 'Admin_Model_Feature',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			),
			'category'=>array(
					'columns' => array('categoryId'),
					'refTableClass' => 'Admin_Model_Category',
					'refColumns' => array('id'),
					'onDelete' => self::CASCADE,
					'onUpdate' => self::CASCADE,
			)
	);

	public function getFeatureDetail($param = NULL, $options = NULL) {
		if ($options == NULL) {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( 'feature_detail AS fd' )
				->join ( 'feature AS f', 'f.id = fd.featureId', array ( 'titleVi AS FeatureName' ) )
				->where ( 'fd.isDelete = 0')
				->where ( 'f.isDelete = 0')
				->group('fd.id');
		} elseif ($options ['task'] == 'admin-product') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( 'feature_detail AS fd' )
				->join ( 'feature AS f', 'f.id = fd.featureId', array ( 'f.titleVi AS FeatureName', 'isMultiImage', 'isDetailProduct' ) )
				->join ( 'product_feature_detail AS pfd', 'fd.id = pfd.featureDetailId')
				->join ( 'category AS c', 'c.id = fd.categoryId', array('c.titleVi AS CategoryName'))
				->where ( 'pfd.productId = ?', ( int ) $param)
				->where ( 'fd.isDelete = 0')
				->where ( 'f.isDelete = 0')
				->where ( 'c.isDelete = 0')
				->group('fd.id');
		} elseif ($options ['task'] == 'admin-category') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( 'feature_detail AS fd' )
				->join ( 'feature AS f', 'f.id = fd.featureId', array ( 'f.titleVi AS FeatureName', 'isMultiImage', 'isDetailProduct' ) )
				->where ( 'fd.categoryId = ?', ( int ) $param)
				->where ( 'fd.isDelete = 0')
				->where ( 'f.isDelete = 0')
				->group('fd.id');
		}elseif ($options ['task'] == 'admin-fetureCategory') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( 'feature_detail AS fd' )				
				->join ( 'category AS c', 'c.id = fd.categoryId', array('c.id AS categoryId', 'c.titleVi AS categoryName'))
				->join ( 'feature AS f', 'f.id = fd.featureId', array ( 'f.titleVi AS FeatureName', 'isMultiImage', 'isDetailProduct' ) )
				->where ( 'fd.isDelete = 0')
				->where ( 'fd.featureId = ?', ( int ) $param )
				->group('fd.id');
		}elseif ($options['task'] == 'admin-search'){
			$zDate = new Zend_Date();
			$featureId = $param['featureId'];
			
			$select = $this->select ()
				->where ( 'isDelete = 0')
				->where ( 'featureId = ?', ( int ) $featureId );
			
			if(isset($param['titleVi'])){
				$select->where ( 'titleVi = ?', $param['titleVi']);
			}
			
			if(isset($param['titleEn'])){
				$select->where ( 'titleEn = ?', $param['titleEn']);
			}
			
			if(isset($param['status'])){
				$select->where ( 'status = ?', $param['status']);
			}
			
			if(isset($param['createFrom']) && isset($param['createTo'])){
				$zDate->set($params['createFrom'], DATETIME_FORMAT_DB);
		        $from = $zDate->toString(DATETIME_FORMAT_DB);
			
		        $zDate->set($params['createTo'], DATETIME_FORMAT_DB);
		        $to = $zDate->toString(DATETIME_FORMAT_DB);
		        
		        $select->where ( "created BETWEEN {$from} AND {$to}");
			}
			
			if(isset($param['modifiedFrom']) && isset($param['modifiedTo'])){
				$zDate->set($params['modifiedFrom'], DATETIME_FORMAT_DB);
		        $from = $zDate->toString(DATETIME_FORMAT_DB);
			
		        $zDate->set($params['modifiedTo'], DATETIME_FORMAT_DB);
		        $to = $zDate->toString(DATETIME_FORMAT_DB);
		        
		        $select->where ( "modified BETWEEN {$from} AND {$to}");
			}
		}
		
		$result = $this->fetchAll ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function getFetchPairs() {
		$query = "SELECT id, titleVi
                    FROM $this->_name
					WHERE isDelete = 0";
		return $this->_db->fetchPairs ( $query );
	}

	public function getFeatureDetailOne($id, $options = null) {
		if ($options == null) {
			$select = $this->select ()
						->where('isDelete = 0')
						->where ( 'id = ?', ( int ) $id );
		} elseif ($options ['task'] == 'feature') {
			$select = $this->select ()
				->setIntegrityCheck ( false )
				->from ( "{$this->_name} AS fd" )
				->join ( 'feature AS f', 'f.id = fd.featureId', array ( 'categoryPath' ) )
				->where ( 'fd.isDelete = 0')
				->where ( 'f.isDelete = 0')
				->where ( 'fd.id = ?', ( int ) $id )
				->group('fd.id');
		}
		
		$result = $this->fetchRow ( $select );
		if ($result) {
			return $result->toArray ();
		} else {
			return null;
		}
	}

	public function addItem($info) {
		try {
			$id = $this->insert ( $info );
			
			return $id;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function updateItem($id, $info) {
		try {			
			$result = $this->update ( $info, "id = " . $id );
			
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}
	
	/**
	 * remove update column isDelete = 1
	 * 
	 * @param string $params
	 * @param string $options
	 * @return boolean
	 */
	public function removeItem($params = null, $options = null) {		
		$data = array('isDelete' => 1);
		
		if($options == null){			
			$result = $this->updateItem($params, $data);
		}elseif($options['task'] == 'remove-select'){
			foreach($params as $key => $val){				
				$result = $this->updateItem($val, $data);
			}
		}elseif($options['task'] == 'remove-all'){
			$params = $this->getFeatureDetail();
				
			foreach($params as $key => $val){				
				$result = $this->updateItem($val['id'], $data);
			}
		}		
		
		return $result;
	}
	
	/**
	 * restore update column isDelete = 0
	 *
	 * @param string $params
	 * @param string $options
	 * @return boolean
	 */
	public function restoreItem($params = null, $options = null) {		
		if($options == null){
			$data = array('isDelete' => 0);
			$result = $this->updateItem($params, $data);
		}elseif($options['task'] == 'restore-select'){
			foreach($params as $key => $val){
				$data = array('isDelete' => 0);
				$result = $this->updateItem($val, $data);
			}
		}elseif($options['task'] == 'restore-all'){
			$params = $this->getFeatureDetail();
	
			foreach($params as $key => $val){
				$data = array('isDelete' => 0);
				$result = $this->updateItem($val, $data);
			}
		}	
		
		return $result;
	}

	public function deleteItem($params = null, $options = null) {
		try {
			if($options == null){
				$featureDetailRowset = $this->find($params);
				$featureDetail = $featureDetailRowset->current();			
				$result = $featureDetail->delete();			
			}elseif($options['task'] == 'delete-select'){
				foreach($params as $key => $val){
					$featureDetailRowset = $this->find($val);
					$featureDetail = $featureDetailRowset->current();
					$result = $featureDetail->delete();
				}			
			}elseif($options['task'] == 'delete-all'){
				$params = $this->getFeatureDetail();
				
				foreach($params as $key => $val){
					$featureDetailRowset = $this->find($val);
					$featureDetail = $featureDetailRowset->current();
					$result = $featureDetail->delete();
				}			
			}
		
			return $result;
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function countItems() {
		$query = $this->select ()
			->from ( $this->_name, 'count(id) as total' );
		$result = $this->fetchRow ( $query );
		if ($result) {
			return $result->total;
		} else {
			return null;
		}
	}
}